package hutool.datasource.oracle;

import cn.hutool.core.date.DateUtil;
import cn.hutool.db.ds.simple.SimpleDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.*;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/5/24 19:05
 * @since JDK 1.8
 */
public class Oracle {



    public static void main(String[] args) {

        List<Map<String, Object>> mapList=selectMapList("select * from \"stu_user\"");
        mapList.forEach(e->{
            System.out.println(e);
        });
        String str[]={"小张","小刘","李四","王五","刘六","王三","刘二","六四","六三","王三","张三"};
        for (int i = 0; i <10000 ; i++) {
            int num=(int)(Math.random()*10);
            String name=str[num];
            String date= DateUtil.now();
            String sql="INSERT INTO \"TEST\".\"stu_user\" VALUES ('"+i+"', '"+name+"', '"+i+"', '学生', '湖北武汉', TO_DATE('"+date +"', 'SYYYY-MM-DD HH24:MI:SS'))";
            sqlExecute(sql);
        }

    }

    /**
     * sql 更新或删除
     * @param sql
     * @return
     */
    public static boolean sqlExecute(String sql){
        boolean flag=false;
        Statement stmt = null;
        Connection conn = null;
        try {
            conn=getConnection();
            stmt = conn.createStatement();
            flag = stmt.execute(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }



    /**
     * 查询集合
     *
     * 根据sql查询集合
     * @param sql
     * @return
     */
    public static List<Map<String, Object>> selectMapList(String sql){
        List<Map<String, Object>> mapList=null;
        ResultSet resultSet=sqlExecuteQuery(sql);
        if(resultSet==null){
            return mapList;
        }
        String[] fields=getFeildList(resultSet);
        try {
            mapList=new ArrayList<>(50);
            while (resultSet.next()){
                Map<String, Object> stringObjectMap=new HashMap<>();
                for (int i = 0; i < fields.length; i++) {
                    String feildName=fields[i];
                    Object value=resultSet.getObject(feildName);
                    stringObjectMap.put(feildName,value);
                }
                mapList.add(stringObjectMap);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return mapList;
    }


    //执行查询sql
    private static ResultSet sqlExecuteQuery(String sql){
        Statement stmt = null;
        Connection conn = null;
        ResultSet rs=null;
        try {
            conn=getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return rs;
    }



    //获取字段列表
    private static String[] getFeildList(ResultSet rs){
        try {
            ResultSetMetaData rsmd = rs.getMetaData();
            String [] lie=new String[rsmd.getColumnCount()];
            for (int i = 1; i <rsmd.getColumnCount()+1; i++) {
                lie[i-1]=rsmd.getColumnName(i);
            }
            return lie;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new String[]{};
    }




    private static Connection getConnection() {
        DataSource ds = new SimpleDataSource("jdbc:oracle:thin:@192.168.217.134:1521/helowin", "test","test","oracle.jdbc.driver.OracleDriver");
        try {
            return ds.getConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
